<?php
/**
 * Created by PhpStorm.
 * User: denh
 * Date: 16/8/18
 * Time: 下午5:32
 */

class BsUserManage extends Base {

    /**
     * 审核医师
     * @param $doctorId int 医师详情id
     * @param $status   string 审核状态 （通过，未通过，待审核，待完善）
     * @param $reason   string 拒绝通过理由
     */
    static function checkUser($doctorId, $status, $reason = '') {
        $doDoctor = new Do医师详情();
        $doDoctor->set_id($doctorId);
        $doDoctor->where($doDoctor->data)->row();

        $userId = $doDoctor->get_用户id();
        $doDoctor->set_状态($status);
        if (!$doDoctor->get_checkTime()) {
            $doDoctor->set_checkTime(date("Y-m-d H:i:s"));
        }

        $res1 = $doDoctor->save();

        if($status == '通过'){
            $authentication = '3';
            $info = Constants::passUserCheck;
        }
        if($status == '待审核' || $status == '待完善'){
            $authentication = '1';
        }
        if($status == '未通过'){
            $authentication = '1';
            $info = "您因以下原因未通过审核：".$reason;
        }

        if (!isset($authentication)) {
            return false;
        }

        $doUser = new Do用户();
        $doUser->set_id($userId);
        $doUser->set_更新缓存('是');
        $doUser->set_认证($authentication);
        $res2 = $doUser->save();

        if ($res1 and $res2) {
            $user_info = $doUser->where($doUser->data)->row();
            $user = $user_info->data;
            if(isset($info)) {
                $bm = new C发微信消息();
                $bm->ff发送医师审核文本消息($info,$user,$status);
            }
            return true;
        }
        return false;
    }

    /**修改用户资料
     * @param int $id 用户id或者openid
     * @param string $id_type
     * @param array $data
     */
    static function modifyUserInfo($id, $id_type='id', array $data) {
        #$name='', $avatar='', $status='', $keshi='', $zhicheng, $hospital
        logger()->logInfo("===call modifyUserInfo=== id:". $id . var_export($data, true));
        $doUser = new Do用户();
        $doDoctor = new Do医师详情();

        if ($id_type != 'id') {
            $id = $doUser->fields("id")->where("openID = '$id'")->getOneField();
            if (!$id) return false;
        }

        if ($data['status'] == '0') {
            $认证 = '1';
            $状态 = '未通过';
        } elseif ($data['status'] == '1') {
            $认证 = '3';
            $状态 = '通过';
        }

        $doUser->set_id($id);
        $data['name'] && $doUser->set_name($data['name']);
        $data['avatar'] && $doUser->set_头像($data['avatar']);
        isset($认证) && $doUser->set_认证($认证);
        $re_1 = $doUser->save();

        $doDoctor->set_用户id($id);
        $doDoctor->where($doDoctor->data)->row();

        isset($状态) && $doDoctor->set_状态($状态);
        $data['name'] && $doDoctor->set_name($data['name']);
        $data['keshi'] && $doDoctor->set_专业领域($data['keshi']);
        $data['hospital'] && $doDoctor->set_医院($data['hospital']);
        $data['zhicheng'] && $doDoctor->set_职务($data['zhicheng']);
        #$doDoctor->set_照片();
        $re_2 = $doDoctor->save();
        if ($re_1 and $re_2) {
            return true;
        }
        return false;
    }

    /**批量修改用户资料
     * @param $type
     * @param $old
     * @param $new
     * @return bool|int
     */
    static function batchModifyUserInfo($type, $old, $new) {

        $old = trim($old);
        $new = trim($new);

        if ($type == 'title') {
            $sql = "update `医师详情` set `职务` = '$new' where trim(`职务`) = '$old'";
        } else if ($type == 'hospital') {
            $sql = "update `医师详情` set `医院` = '$new' where trim(`医院`) = '$old'";
        } else if ($type == 'sector') {
            $sql = "update `医师详情` set `专业领域` = '$new' where trim(`专业领域`) = '$old'";
        } else {
            return false;
        }
        logger()->logInfo("batchModifyUserInfo_sql:".$sql);

        return pdo()->exec($sql);
    }

}